<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="robots" content="noodp" />
        <title>C&#43;&#43;与python文件系统对比 - L_B__</title><meta name="referrer" content="no-referrer">
<meta name="description" content="C&#43;&#43;与python文件系统对比"><meta property="og:title" content="C&#43;&#43;与python文件系统对比" />
<meta property="og:description" content="C&#43;&#43;与python文件系统对比" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://acking-you.github.io/posts/c&#43;&#43;%E4%B8%8Epython%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F%E5%AF%B9%E6%AF%94/" /><meta property="og:image" content="https://acking-you.github.io/logo.png"/><meta property="article:section" content="posts" />
<meta property="article:published_time" content="2022-01-25T00:00:00+00:00" />
<meta property="article:modified_time" content="2022-01-25T00:00:00+00:00" />

<meta name="twitter:card" content="summary_large_image"/>
<meta name="twitter:image" content="https://acking-you.github.io/logo.png"/>

<meta name="twitter:title" content="C&#43;&#43;与python文件系统对比"/>
<meta name="twitter:description" content="C&#43;&#43;与python文件系统对比"/>
<meta name="application-name" content="FeelIt">
<meta name="apple-mobile-web-app-title" content="FeelIt"><meta name="theme-color" content="#ffffff"><meta name="msapplication-TileColor" content="#da532c"><link rel="canonical" href="https://acking-you.github.io/posts/c&#43;&#43;%E4%B8%8Epython%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F%E5%AF%B9%E6%AF%94/" /><link rel="prev" href="https://acking-you.github.io/posts/java-io/" /><link rel="next" href="https://acking-you.github.io/posts/tcp%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B%E4%B8%ADconnectlistenaccept%E4%B8%89%E8%80%85%E4%B9%8B%E9%97%B4%E7%9A%84%E5%85%B3%E7%B3%BB/" /><link rel="stylesheet" href="/css/page.min.css"><link rel="stylesheet" href="/css/home.min.css"><script type="application/ld+json">
    {
        "@context": "http://schema.org",
        "@type": "BlogPosting",
        "headline": "C++与python文件系统对比",
        "inLanguage": "zh-CN",
        "mainEntityOfPage": {
            "@type": "WebPage",
            "@id": "https:\/\/acking-you.github.io\/posts\/c\u002b\u002b%E4%B8%8Epython%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F%E5%AF%B9%E6%AF%94\/"
        },"genre": "posts","keywords": "C\u002b\u002b与python文件系统对比","wordcount":  3343 ,
        "url": "https:\/\/acking-you.github.io\/posts\/c\u002b\u002b%E4%B8%8Epython%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F%E5%AF%B9%E6%AF%94\/","datePublished": "2022-01-25T00:00:00+00:00","dateModified": "2022-01-25T00:00:00+00:00","publisher": {
            "@type": "Organization",
            "name": "作者"},"author": {
                "@type": "Person",
                "name": "作者"
            },"description": "C++与python文件系统对比"
    }
    </script></head><body data-header-desktop="auto" data-header-mobile="auto"><script>(window.localStorage && localStorage.getItem('theme') ? localStorage.getItem('theme') === 'dark' : ('auto' === 'auto' ? window.matchMedia('(prefers-color-scheme: dark)').matches : 'auto' === 'dark')) && document.body.setAttribute('theme', 'dark');</script>

        <div id="mask"></div><div class="wrapper"><header class="desktop" id="header-desktop">
    <div class="header-wrapper">
        <div class="header-title">
            <a href="/" title="L_B__">L_B__</a>
        </div>
        <div class="menu">
            <div class="menu-inner"><a class="menu-item" href="/posts/"> 文章 </a><a class="menu-item" href="/tags/"> 标签 </a><a class="menu-item" href="/categories/"> 分类 </a><span class="menu-item delimiter"></span><span class="menu-item search" id="search-desktop">
                        <input type="text" placeholder="搜索文章标题或内容..." id="search-input-desktop">
                        <a href="#" class="search-button search-toggle" id="search-toggle-desktop" title="搜索">
                            <i class="fas fa-search fa-fw"></i>
                        </a>
                        <a href="#" class="search-button search-clear" id="search-clear-desktop" title="清空">
                            <i class="fas fa-times-circle fa-fw"></i>
                        </a>
                        <span class="search-button search-loading" id="search-loading-desktop">
                            <i class="fas fa-spinner fa-fw fa-spin"></i>
                        </span>
                    </span><a href="javascript:void(0);" class="menu-item theme-switch" title="切换主题">
                    <i class="fas fa-adjust fa-fw"></i>
                </a>
            </div>
        </div>
    </div>
</header><header class="mobile" id="header-mobile">
    <div class="header-container">
        <div class="header-wrapper">
            <div class="header-title">
                <a href="/" title="L_B__">L_B__</a>
            </div>
            <div class="menu-toggle" id="menu-toggle-mobile">
                <span></span><span></span><span></span>
            </div>
        </div>
        <div class="menu" id="menu-mobile"><div class="search-wrapper">
                    <div class="search mobile" id="search-mobile">
                        <input type="text" placeholder="搜索文章标题或内容..." id="search-input-mobile">
                        <a href="#" class="search-button search-toggle" id="search-toggle-mobile" title="搜索">
                            <i class="fas fa-search fa-fw"></i>
                        </a>
                        <a href="#" class="search-button search-clear" id="search-clear-mobile" title="清空">
                            <i class="fas fa-times-circle fa-fw"></i>
                        </a>
                        <span class="search-button search-loading" id="search-loading-mobile">
                            <i class="fas fa-spinner fa-fw fa-spin"></i>
                        </span>
                    </div>
                    <a href="#" class="search-cancel" id="search-cancel-mobile">
                        取消
                    </a>
                </div><a class="menu-item" href="/posts/" title="">文章</a><a class="menu-item" href="/tags/" title="">标签</a><a class="menu-item" href="/categories/" title="">分类</a><div class="menu-item"><a href="javascript:void(0);" class="theme-switch" title="切换主题">
                    <i class="fas fa-adjust fa-fw"></i>
                </a>
            </div></div>
    </div>
</header><div class="search-dropdown desktop">
    <div id="search-dropdown-desktop"></div>
</div>
<div class="search-dropdown mobile">
    <div id="search-dropdown-mobile"></div>
</div><main class="main">
                <div class="container"><div class="toc" id="toc-auto">
            <h2 class="toc-title">目录</h2>
            <div class="toc-content" id="toc-content-auto"></div>
        </div><article class="page single" data-toc="disable"><div class="featured-image"><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://img-blog.csdnimg.cn/img_convert/2319540b52ba53f88ff0afbf5cabb92c.png#pic_center"
        data-srcset="https://img-blog.csdnimg.cn/img_convert/2319540b52ba53f88ff0afbf5cabb92c.png#pic_center, https://img-blog.csdnimg.cn/img_convert/2319540b52ba53f88ff0afbf5cabb92c.png#pic_center 1.5x, https://img-blog.csdnimg.cn/img_convert/2319540b52ba53f88ff0afbf5cabb92c.png#pic_center 2x"
        data-sizes="auto"
        alt="https://img-blog.csdnimg.cn/img_convert/2319540b52ba53f88ff0afbf5cabb92c.png#pic_center"
        title="C&#43;&#43;与python文件系统对比" /></div><div class="single-card" data-image="true"><h2 class="single-title animated flipInX">C&#43;&#43;与python文件系统对比</h2><div class="post-meta">
                <div class="post-meta-line"><span class="post-author"><a href="/" title="Author" rel=" author" class="author"><i class="fas fa-user-circle fa-fw"></i>作者</a></span>&nbsp;<span class="post-category">出版于  <a href="/categories/%E7%8E%B0%E4%BB%A3c++%E8%AF%AD%E6%B3%95/"><i class="far fa-folder fa-fw"></i>现代C++语法</a></span></div>
                <div class="post-meta-line"><span><i class="far fa-calendar-alt fa-fw"></i>&nbsp;<time datetime="2022-01-25">2022-01-25</time></span>&nbsp;<span><i class="fas fa-pencil-alt fa-fw"></i>&nbsp;约 3343 字</span>&nbsp;
                    <span><i class="far fa-clock fa-fw"></i>&nbsp;预计阅读 7 分钟</span>&nbsp;</div>
            </div>
            
            <hr><div class="details toc" id="toc-static"  data-kept="">
                    <div class="details-summary toc-title">
                        <span>目录</span>
                        <span><i class="details-icon fas fa-angle-right"></i></span>
                    </div>
                    <div class="details-content toc-content" id="toc-content-static"><nav id="TableOfContents">
  <ul>
    <li><a href="#filesystempath-vs-ospath"><code>filesystem::path</code> vs. <code>os.path</code></a>
      <ul>
        <li><a href="#判断是否为绝对路径">判断是否为绝对路径</a></li>
        <li><a href="#路径分割">路径分割</a></li>
        <li><a href="#路径拼接">路径拼接</a></li>
      </ul>
    </li>
    <li><a href="#filesystem-vs-os"><code>filesystem</code> vs. <code>os</code></a>
      <ul>
        <li><a href="#获取当前工作目录">获取当前工作目录</a></li>
        <li><a href="#返回指定目录下的所有文件文件夹">返回指定目录下的所有文件/文件夹</a></li>
        <li><a href="#递归返回指定目录下的所有文件文件夹">递归返回指定目录下的所有文件/文件夹</a></li>
        <li><a href="#判断路径是否存在">判断路径是否存在</a></li>
        <li><a href="#判断路径是文件还是目录">判断路径是文件还是目录</a></li>
        <li><a href="#返回绝对路径神奇">返回绝对路径【神奇】</a></li>
      </ul>
    </li>
    <li><a href="#filesystem-vs-shutil"><code>filesystem</code> vs. <code>shutil</code></a>
      <ul>
        <li><a href="#文件拷贝">文件拷贝</a></li>
        <li><a href="#文件删除">文件删除</a></li>
        <li><a href="#路径拷贝">路径拷贝</a></li>
        <li><a href="#递归删除">递归删除</a></li>
      </ul>
    </li>
  </ul>
</nav></div>
                </div><div class="content" id="content"><h1 id="c17-和-python-中好用的文件操作--filesystem--os--shutil">C++17 和 python 中好用的文件操作 | filesystem | os | shutil</h1>
<table>
<thead>
<tr>
<th style="text-align:left">C++ 17</th>
<th style="text-align:left">python</th>
<th style="text-align:left">功能</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left"><code>filesystem::path::is_absolute()</code></td>
<td style="text-align:left"><code>os.path.isabs()</code></td>
<td style="text-align:left">判断是否为绝对路径</td>
</tr>
<tr>
<td style="text-align:left"><code>filesystem::path::parent_path()</code></td>
<td style="text-align:left"><code>os.path.dirname()</code></td>
<td style="text-align:left">路径分割</td>
</tr>
<tr>
<td style="text-align:left"><code>filesystem::path::filename()</code></td>
<td style="text-align:left"><code>os.path.basename()</code></td>
<td style="text-align:left">路径分割</td>
</tr>
<tr>
<td style="text-align:left"><code>filesystem::operator/()</code></td>
<td style="text-align:left"><code>os.path.join()</code></td>
<td style="text-align:left">路径拼接</td>
</tr>
<tr>
<td style="text-align:left"><code>filesystem::current_path()</code></td>
<td style="text-align:left"><code>os.getcwd()</code></td>
<td style="text-align:left">获取当前路径</td>
</tr>
<tr>
<td style="text-align:left"><code>filesystem::directory_iterator</code></td>
<td style="text-align:left"><code>os.listdir()</code></td>
<td style="text-align:left">返回指定目录下的所有文件/文件夹</td>
</tr>
<tr>
<td style="text-align:left"><code>filesystem::recursive_directory_iterator</code></td>
<td style="text-align:left"><code>os.walk()</code></td>
<td style="text-align:left">递归返回指定目录下的所有文件/文件夹</td>
</tr>
<tr>
<td style="text-align:left"><code>filesystem::exists()</code></td>
<td style="text-align:left"><code>os.path.exists()</code></td>
<td style="text-align:left">判断路径是否存在</td>
</tr>
<tr>
<td style="text-align:left"><code>filesystem::is_regular_file()</code></td>
<td style="text-align:left"><code>os.path.isfile()</code></td>
<td style="text-align:left">判断路径是文件还是目录</td>
</tr>
<tr>
<td style="text-align:left"><code>filesystem::is_directory()</code></td>
<td style="text-align:left"><code>os.path.isdir()</code></td>
<td style="text-align:left">判断路径是文件还是目录</td>
</tr>
<tr>
<td style="text-align:left"><code>filesystem::absolute()</code></td>
<td style="text-align:left"><code>os.path.abspath()</code></td>
<td style="text-align:left">返回绝对路径</td>
</tr>
<tr>
<td style="text-align:left"><code>filesystem::copy_file()</code></td>
<td style="text-align:left"><code>shutil.copyfile()</code></td>
<td style="text-align:left">文件拷贝</td>
</tr>
<tr>
<td style="text-align:left"><code>filesystem::remove()</code></td>
<td style="text-align:left"><code>os.remove()</code></td>
<td style="text-align:left">文件删除</td>
</tr>
<tr>
<td style="text-align:left"><code>filesystem::copy()</code></td>
<td style="text-align:left"><code>shutil.copytree</code></td>
<td style="text-align:left">路径拷贝</td>
</tr>
<tr>
<td style="text-align:left"><code>filesystem::remove_all</code></td>
<td style="text-align:left"><code>shutil.rmtree()</code></td>
<td style="text-align:left">路径删除</td>
</tr>
</tbody>
</table>
<h2 id="filesystempath-vs-ospath"><code>filesystem::path</code> vs. <code>os.path</code></h2>
<ol>
<li><code>filesystem::path</code>是一个类，里面封装了很多方法，我们通过实例化之后直接调用方法。</li>
<li><code>os.path</code>是一个模块，里面有很多函数，可以直接调用。</li>
</ol>
<h3 id="判断是否为绝对路径">判断是否为绝对路径</h3>
<p>什么是绝对路径？我个人的理解是从根目录开始的就是绝对路径，例如<code>/usr/local</code>和<code>C:\\Users</code>，其余都是相对路径。可以发现，在不同操作系统中路径的分割符是不同的。同时在相对路径中<code>./</code>和<code>../</code>有特殊含义，<code>./</code>表示当前目录，<code>../</code>表示上一层目录，相应地，<code>../../</code>就是上两层目录。</p>
<p>1.<code>filesystem::path</code>中提供了判断是否为绝对路径/相对路径方法。</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-C++" data-lang="C++">  <span class="n">_LIBCPP_INLINE_VISIBILITY</span> <span class="kt">bool</span> <span class="nf">is_absolute</span><span class="p">()</span> <span class="k">const</span> <span class="p">{</span>
    <span class="k">return</span> <span class="n">has_root_directory</span><span class="p">();</span>
  <span class="p">}</span>
  <span class="n">_LIBCPP_INLINE_VISIBILITY</span> <span class="kt">bool</span> <span class="nf">is_relative</span><span class="p">()</span> <span class="k">const</span> <span class="p">{</span> <span class="k">return</span> <span class="o">!</span><span class="n">is_absolute</span><span class="p">();</span> <span class="p">}</span>
</code></pre></div><p>可以发现，判断相对路径的结果就是绝对路径取反。</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-C++" data-lang="C++"><span class="kt">void</span> <span class="nf">eg1_1</span><span class="p">()</span> <span class="p">{</span>
    <span class="cm">/*判断是否为绝对路径*/</span>
    <span class="c1">// std::filesystem::path abs_path = &#34;C:\\Users&#34;;
</span><span class="c1"></span>    <span class="n">std</span><span class="o">::</span><span class="n">filesystem</span><span class="o">::</span><span class="n">path</span> <span class="n">abs_path</span> <span class="o">=</span> <span class="s">&#34;/usr/local&#34;</span><span class="p">;</span>  <span class="c1">// 注意，实例化path的时候可以直接用等号
</span><span class="c1"></span>    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;abs_path.is_absolute() : &#34;</span> <span class="o">&lt;&lt;</span> <span class="n">abs_path</span><span class="p">.</span><span class="n">is_absolute</span><span class="p">()</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;abs_path.is_relative() : &#34;</span> <span class="o">&lt;&lt;</span> <span class="n">abs_path</span><span class="p">.</span><span class="n">is_relative</span><span class="p">()</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>

    <span class="n">std</span><span class="o">::</span><span class="n">filesystem</span><span class="o">::</span><span class="n">path</span> <span class="n">rel_path</span> <span class="o">=</span> <span class="s">&#34;../&#34;</span><span class="p">;</span>
    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;rel_path.is_absolute() : &#34;</span> <span class="o">&lt;&lt;</span> <span class="n">rel_path</span><span class="p">.</span><span class="n">is_absolute</span><span class="p">()</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;rel_path.is_relative() : &#34;</span> <span class="o">&lt;&lt;</span> <span class="n">rel_path</span><span class="p">.</span><span class="n">is_relative</span><span class="p">()</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell">abs_path.is_absolute<span class="o">()</span> : <span class="m">1</span>
abs_path.is_relative<span class="o">()</span> : <span class="m">0</span>
rel_path.is_absolute<span class="o">()</span> : <span class="m">0</span>
rel_path.is_relative<span class="o">()</span> : <span class="m">1</span>
</code></pre></div><ol start="2">
<li><code>os.path</code>中提供了<code>isabs()</code>函数用于判断是否为绝对路径。</li>
</ol>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="k">def</span> <span class="nf">eg1_1</span><span class="p">():</span>
    <span class="s2">&#34;&#34;&#34;判断是否为绝对路径&#34;&#34;&#34;</span>
    <span class="c1"># abs_path = &#34;C:\\Users&#34;</span>
    <span class="n">abs_path</span> <span class="o">=</span> <span class="s2">&#34;/usr/local&#34;</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&#34;os.path.isabs(</span><span class="si">{}</span><span class="s2">) : </span><span class="si">{}</span><span class="s2">&#34;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">abs_path</span><span class="p">,</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isabs</span><span class="p">(</span><span class="n">abs_path</span><span class="p">)))</span>
    <span class="n">rel_path</span> <span class="o">=</span> <span class="s2">&#34;../&#34;</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&#34;os.path.isabs(</span><span class="si">{}</span><span class="s2">) : </span><span class="si">{}</span><span class="s2">&#34;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">rel_path</span><span class="p">,</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isabs</span><span class="p">(</span><span class="n">rel_path</span><span class="p">)))</span>
</code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell">os.path.isabs<span class="o">(</span>/usr/local<span class="o">)</span> : True
os.path.isabs<span class="o">(</span>../<span class="o">)</span> : False
</code></pre></div><h3 id="路径分割">路径分割</h3>
<ol>
<li><code>filesystem::path</code>中提供了路径分割的方法。</li>
</ol>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-C++" data-lang="C++">  <span class="c1">// decomposition
</span><span class="c1"></span>  <span class="n">_LIBCPP_INLINE_VISIBILITY</span> <span class="n">path</span> <span class="nf">root_name</span><span class="p">()</span> <span class="k">const</span> <span class="p">{</span>
    <span class="k">return</span> <span class="n">string_type</span><span class="p">(</span><span class="n">__root_name</span><span class="p">());</span>
  <span class="p">}</span>
  <span class="n">_LIBCPP_INLINE_VISIBILITY</span> <span class="n">path</span> <span class="nf">root_directory</span><span class="p">()</span> <span class="k">const</span> <span class="p">{</span>
    <span class="k">return</span> <span class="n">string_type</span><span class="p">(</span><span class="n">__root_directory</span><span class="p">());</span>
  <span class="p">}</span>
  <span class="n">_LIBCPP_INLINE_VISIBILITY</span> <span class="n">path</span> <span class="nf">root_path</span><span class="p">()</span> <span class="k">const</span> <span class="p">{</span>
    <span class="k">return</span> <span class="n">root_name</span><span class="p">().</span><span class="n">append</span><span class="p">(</span><span class="n">string_type</span><span class="p">(</span><span class="n">__root_directory</span><span class="p">()));</span>
  <span class="p">}</span>
  <span class="n">_LIBCPP_INLINE_VISIBILITY</span> <span class="n">path</span> <span class="nf">relative_path</span><span class="p">()</span> <span class="k">const</span> <span class="p">{</span>
    <span class="k">return</span> <span class="n">string_type</span><span class="p">(</span><span class="n">__relative_path</span><span class="p">());</span>
  <span class="p">}</span>
  <span class="n">_LIBCPP_INLINE_VISIBILITY</span> <span class="n">path</span> <span class="nf">parent_path</span><span class="p">()</span> <span class="k">const</span> <span class="p">{</span>
    <span class="k">return</span> <span class="n">string_type</span><span class="p">(</span><span class="n">__parent_path</span><span class="p">());</span>
  <span class="p">}</span>
  <span class="n">_LIBCPP_INLINE_VISIBILITY</span> <span class="n">path</span> <span class="nf">filename</span><span class="p">()</span> <span class="k">const</span> <span class="p">{</span>
    <span class="k">return</span> <span class="n">string_type</span><span class="p">(</span><span class="n">__filename</span><span class="p">());</span>
  <span class="p">}</span>
  <span class="n">_LIBCPP_INLINE_VISIBILITY</span> <span class="n">path</span> <span class="nf">stem</span><span class="p">()</span> <span class="k">const</span> <span class="p">{</span> <span class="k">return</span> <span class="n">string_type</span><span class="p">(</span><span class="n">__stem</span><span class="p">());</span> <span class="p">}</span>
  <span class="n">_LIBCPP_INLINE_VISIBILITY</span> <span class="n">path</span> <span class="nf">extension</span><span class="p">()</span> <span class="k">const</span> <span class="p">{</span>
    <span class="k">return</span> <span class="n">string_type</span><span class="p">(</span><span class="n">__extension</span><span class="p">());</span>
  <span class="p">}</span>
</code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-C++" data-lang="C++"><span class="kt">void</span> <span class="nf">eg1_2</span><span class="p">()</span> <span class="p">{</span>
    <span class="cm">/*路径分割*/</span>
    <span class="n">std</span><span class="o">::</span><span class="n">filesystem</span><span class="o">::</span><span class="n">path</span> <span class="n">path</span> <span class="o">=</span> <span class="s">&#34;../test_dir/1.txt&#34;</span><span class="p">;</span>
    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;path.relative_path() : &#34;</span> <span class="o">&lt;&lt;</span> <span class="n">path</span><span class="p">.</span><span class="n">relative_path</span><span class="p">()</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;path.parent_path() : &#34;</span> <span class="o">&lt;&lt;</span> <span class="n">path</span><span class="p">.</span><span class="n">parent_path</span><span class="p">()</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;path.filename() : &#34;</span> <span class="o">&lt;&lt;</span> <span class="n">path</span><span class="p">.</span><span class="n">filename</span><span class="p">()</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;path.stem() : &#34;</span> <span class="o">&lt;&lt;</span> <span class="n">path</span><span class="p">.</span><span class="n">stem</span><span class="p">()</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;path.extension() : &#34;</span> <span class="o">&lt;&lt;</span> <span class="n">path</span><span class="p">.</span><span class="n">extension</span><span class="p">()</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell">path.relative_path<span class="o">()</span> : <span class="s2">&#34;../test_dir/1.txt&#34;</span>
path.parent_path<span class="o">()</span> : <span class="s2">&#34;../test_dir&#34;</span>
path.filename<span class="o">()</span> : <span class="s2">&#34;1.txt&#34;</span>
path.stem<span class="o">()</span> : <span class="s2">&#34;1&#34;</span>
path.extension<span class="o">()</span> : <span class="s2">&#34;.txt&#34;</span>
</code></pre></div><ol>
<li><code>os.path</code>中提供了分割函数<code>split()</code>以及<code>dirname()</code>，<code>basename()</code>。</li>
</ol>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="k">def</span> <span class="nf">eg1_2</span><span class="p">():</span>
    <span class="s2">&#34;&#34;&#34;路径分割&#34;&#34;&#34;</span>
    <span class="n">path</span> <span class="o">=</span> <span class="s2">&#34;../test_dir/1.txt&#34;</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&#34;os.path.split(path) : </span><span class="si">{}</span><span class="s2">&#34;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="n">path</span><span class="p">)))</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&#34;os.path.dirname(path) : </span><span class="si">{}</span><span class="s2">&#34;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">path</span><span class="p">)))</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&#34;os.path.basename(path) : </span><span class="si">{}</span><span class="s2">&#34;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="n">path</span><span class="p">)))</span>
</code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell">os.path.split<span class="o">(</span>path<span class="o">)</span> : <span class="o">(</span><span class="s1">&#39;../test_dir&#39;</span>, <span class="s1">&#39;1.txt&#39;</span><span class="o">)</span>
os.path.dirname<span class="o">(</span>path<span class="o">)</span> : ../test_dir
os.path.basename<span class="o">(</span>path<span class="o">)</span> : 1.txt
</code></pre></div><h3 id="路径拼接">路径拼接</h3>
<ol>
<li><code>filesystem::path</code>中重载了符号<code>/</code>和<code>/=</code>。</li>
</ol>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-C++" data-lang="C++">  <span class="k">friend</span> <span class="n">_LIBCPP_INLINE_VISIBILITY</span> <span class="n">path</span> <span class="k">operator</span><span class="o">/</span><span class="p">(</span><span class="k">const</span> <span class="n">path</span><span class="o">&amp;</span> <span class="n">__lhs</span><span class="p">,</span>
                                                  <span class="k">const</span> <span class="n">path</span><span class="o">&amp;</span> <span class="n">__rhs</span><span class="p">)</span> <span class="p">{</span>
    <span class="n">path</span> <span class="nf">__result</span><span class="p">(</span><span class="n">__lhs</span><span class="p">);</span>
    <span class="n">__result</span> <span class="o">/=</span> <span class="n">__rhs</span><span class="p">;</span>
    <span class="k">return</span> <span class="n">__result</span><span class="p">;</span>
  <span class="p">}</span>
</code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-C++" data-lang="C++"><span class="kt">void</span> <span class="nf">eg1_3</span><span class="p">()</span> <span class="p">{</span>
    <span class="cm">/*路径拼接*/</span>
    <span class="n">std</span><span class="o">::</span><span class="n">filesystem</span><span class="o">::</span><span class="n">path</span> <span class="n">path</span> <span class="o">=</span> <span class="s">&#34;../test_dir&#34;</span><span class="p">;</span>
    <span class="n">std</span><span class="o">::</span><span class="n">filesystem</span><span class="o">::</span><span class="n">path</span> <span class="n">txt1_path</span> <span class="o">=</span> <span class="n">path</span> <span class="o">/</span> <span class="s">&#34;1.txt&#34;</span><span class="p">;</span>
    <span class="n">std</span><span class="o">::</span><span class="n">filesystem</span><span class="o">::</span><span class="n">path</span> <span class="n">txt2_path</span> <span class="o">=</span> <span class="n">path</span> <span class="o">/</span> <span class="s">&#34;1_dir&#34;</span> <span class="o">/</span> <span class="s">&#34;2.txt&#34;</span><span class="p">;</span>
    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;txt1_path : &#34;</span> <span class="o">&lt;&lt;</span> <span class="n">txt1_path</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;txt2_path : &#34;</span> <span class="o">&lt;&lt;</span> <span class="n">txt2_path</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
    <span class="n">path</span> <span class="o">/=</span> <span class="s">&#34;1.txt&#34;</span><span class="p">;</span>
    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;path : &#34;</span> <span class="o">&lt;&lt;</span> <span class="n">path</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell">txt1_path : <span class="s2">&#34;../test_dir/1.txt&#34;</span>
txt2_path : <span class="s2">&#34;../test_dir/1_dir/2.txt&#34;</span>
path : <span class="s2">&#34;../test_dir/1.txt&#34;</span>
</code></pre></div><ol start="2">
<li><code>os.path</code>中提供了<code>join()</code>函数。</li>
</ol>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="k">def</span> <span class="nf">eg1_3</span><span class="p">():</span>
    <span class="s2">&#34;&#34;&#34;路径拼接&#34;&#34;&#34;</span>
    <span class="n">path</span> <span class="o">=</span> <span class="s2">&#34;../test_dir&#34;</span>
    <span class="n">txt1_path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="s2">&#34;1.txt&#34;</span><span class="p">)</span>
    <span class="n">txt2_path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="s2">&#34;1_dir&#34;</span><span class="p">,</span> <span class="s2">&#34;2.txt&#34;</span><span class="p">)</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&#34;txt1_path : </span><span class="si">{}</span><span class="s2">&#34;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">txt1_path</span><span class="p">))</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&#34;txt2_path : </span><span class="si">{}</span><span class="s2">&#34;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">txt2_path</span><span class="p">))</span>
</code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell">txt1_path : ../test_dir/1.txt
txt2_path : ../test_dir/1_dir/2.txt
</code></pre></div><ol start="3">
<li>注意，不同操作系统的分隔符不同，所以在Windows中运行结果如下。</li>
</ol>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell">txt1_path : ../test_dir<span class="se">\1</span>.txt
txt2_path : ../test_dir<span class="se">\1</span>_dir<span class="se">\2</span>.txt
</code></pre></div><h2 id="filesystem-vs-os"><code>filesystem</code> vs. <code>os</code></h2>
<h3 id="获取当前工作目录">获取当前工作目录</h3>
<ol>
<li><code>filesystem</code>提供了获取当前路径的函数<code>current_path()</code>，注意返回的是绝对路径。</li>
</ol>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-C++" data-lang="C++"><span class="kt">void</span> <span class="nf">eg2_1</span><span class="p">()</span> <span class="p">{</span>
    <span class="cm">/*获取当前路径*/</span>
    <span class="n">std</span><span class="o">::</span><span class="n">filesystem</span><span class="o">::</span><span class="n">path</span> <span class="n">current_path</span> <span class="o">=</span> <span class="n">std</span><span class="o">::</span><span class="n">filesystem</span><span class="o">::</span><span class="n">current_path</span><span class="p">();</span>
    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;current_path : &#34;</span> <span class="o">&lt;&lt;</span> <span class="n">current_path</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell">current_path : <span class="s2">&#34;/Users/xxx/Github/intro_to_C-python/xxx/cmake-build-debug&#34;</span>
</code></pre></div><ol start="2">
<li><code>os</code>提供了函数<code>getcwd()</code>。</li>
</ol>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="k">def</span> <span class="nf">eg2_1</span><span class="p">():</span>
    <span class="s2">&#34;&#34;&#34;获取当前路径&#34;&#34;&#34;</span>
    <span class="n">current_path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">getcwd</span><span class="p">()</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&#34;current_path : </span><span class="si">{}</span><span class="s2">&#34;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">current_path</span><span class="p">))</span>
</code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell">current_path : /Users/xxx/Github/intro_to_C-python/xxx
</code></pre></div><p>这里先展示一下目录树，方便理解后边的例子。</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell">└── test_dir
    ├── 1.txt
    └── 1_dir
        ├── 2.txt
        └── empty_dir
</code></pre></div><h3 id="返回指定目录下的所有文件文件夹">返回指定目录下的所有文件/文件夹</h3>
<ol>
<li><code>filesystem</code>的类<code>directory_iterator</code>可以实现该功能。</li>
</ol>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-C++" data-lang="C++"><span class="kt">void</span> <span class="nf">eg2_2</span><span class="p">()</span> <span class="p">{</span>
    <span class="cm">/*返回指定目录下的所有文件/文件夹*/</span>
    <span class="n">std</span><span class="o">::</span><span class="n">filesystem</span><span class="o">::</span><span class="n">directory_iterator</span> <span class="n">iter</span><span class="p">(</span><span class="s">&#34;../test_dir&#34;</span><span class="p">);</span>
    <span class="k">for</span><span class="p">(</span><span class="k">const</span> <span class="k">auto</span> <span class="o">&amp;</span><span class="nl">i</span> <span class="p">:</span> <span class="n">iter</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">i</span><span class="p">.</span><span class="n">path</span><span class="p">()</span> <span class="o">&lt;&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="s2">&#34;../test_dir/1.txt&#34;</span>
<span class="s2">&#34;../test_dir/1_dir&#34;</span>
</code></pre></div><ol start="2">
<li><code>os</code>中提供了函数<code>listdir()</code>。</li>
</ol>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="k">def</span> <span class="nf">eg2_2</span><span class="p">():</span>
    <span class="s2">&#34;&#34;&#34;返回指定目录下的所有文件/文件夹&#34;&#34;&#34;</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="s2">&#34;./test_dir&#34;</span><span class="p">):</span>
        <span class="nb">print</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
</code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell">1.txt
1_dir
</code></pre></div><h3 id="递归返回指定目录下的所有文件文件夹">递归返回指定目录下的所有文件/文件夹</h3>
<ol>
<li><code>filesystem</code>的类<code>recursive_directory_iterator</code>可以实现该功能。</li>
</ol>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-C++" data-lang="C++"><span class="kt">void</span> <span class="nf">eg2_3</span><span class="p">()</span> <span class="p">{</span>
    <span class="cm">/*递归返回指定目录下的所有文件/文件夹*/</span>
    <span class="n">std</span><span class="o">::</span><span class="n">filesystem</span><span class="o">::</span><span class="n">recursive_directory_iterator</span> <span class="n">iter</span><span class="p">(</span><span class="s">&#34;../test_dir&#34;</span><span class="p">);</span>
    <span class="k">for</span><span class="p">(</span><span class="k">const</span> <span class="k">auto</span> <span class="o">&amp;</span><span class="nl">i</span> <span class="p">:</span> <span class="n">iter</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">i</span><span class="p">.</span><span class="n">path</span><span class="p">()</span> <span class="o">&lt;&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="s2">&#34;../test_dir/1.txt&#34;</span>
<span class="s2">&#34;../test_dir/1_dir&#34;</span>
<span class="s2">&#34;../test_dir/1_dir/empty_dir&#34;</span>
<span class="s2">&#34;../test_dir/1_dir/2.txt&#34;</span>
</code></pre></div><ol start="2">
<li><code>os</code>中提供了函数<code>walk()</code>。</li>
</ol>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="k">def</span> <span class="nf">eg2_3</span><span class="p">():</span>
    <span class="s2">&#34;&#34;&#34;递归返回指定目录下的所有文件/文件夹&#34;&#34;&#34;</span>
    <span class="k">for</span> <span class="n">root</span><span class="p">,</span> <span class="n">dirs</span><span class="p">,</span> <span class="n">files</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">walk</span><span class="p">(</span><span class="s2">&#34;./test_dir&#34;</span><span class="p">):</span>
        <span class="nb">print</span><span class="p">(</span><span class="s2">&#34;root : </span><span class="si">{}</span><span class="s2">, dirs : </span><span class="si">{}</span><span class="s2">, files : </span><span class="si">{}</span><span class="s2">&#34;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">root</span><span class="p">,</span> <span class="n">dirs</span><span class="p">,</span> <span class="n">files</span><span class="p">))</span>
</code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell">root : ./test_dir, <span class="nb">dirs</span> : <span class="o">[</span><span class="s1">&#39;1_dir&#39;</span><span class="o">]</span>, files : <span class="o">[</span><span class="s1">&#39;1.txt&#39;</span><span class="o">]</span>
root : ./test_dir/1_dir, <span class="nb">dirs</span> : <span class="o">[</span><span class="s1">&#39;empty_dir&#39;</span><span class="o">]</span>, files : <span class="o">[</span><span class="s1">&#39;2.txt&#39;</span><span class="o">]</span>
root : ./test_dir/1_dir/empty_dir, <span class="nb">dirs</span> : <span class="o">[]</span>, files : <span class="o">[]</span>
</code></pre></div><h3 id="判断路径是否存在">判断路径是否存在</h3>
<ol>
<li><code>filesystem</code>提供了判断路径是否存在的函数<code>exists()</code>。</li>
</ol>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-C++" data-lang="C++"><span class="kt">void</span> <span class="nf">eg2_4</span><span class="p">()</span> <span class="p">{</span>
    <span class="cm">/*判断路径是否存在*/</span>
    <span class="kt">bool</span> <span class="n">exist</span> <span class="o">=</span> <span class="n">std</span><span class="o">::</span><span class="n">filesystem</span><span class="o">::</span><span class="n">exists</span><span class="p">(</span><span class="s">&#34;C:</span><span class="se">\\</span><span class="s">Users&#34;</span><span class="p">);</span>
    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;exist : &#34;</span> <span class="o">&lt;&lt;</span> <span class="n">exist</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell">exist : <span class="m">0</span>
</code></pre></div><ol start="2">
<li><code>os.path</code>中提供了函数<code>exists()</code>。</li>
</ol>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="k">def</span> <span class="nf">eg2_4</span><span class="p">():</span>
    <span class="s2">&#34;&#34;&#34;判断路径是否存在&#34;&#34;&#34;</span>
    <span class="n">path</span> <span class="o">=</span> <span class="s2">&#34;C:</span><span class="se">\\</span><span class="s2">Users&#34;</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&#34;os.path.exists(</span><span class="si">{}</span><span class="s2">) : </span><span class="si">{}</span><span class="s2">&#34;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">path</span><span class="p">)))</span>
</code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell">os.path.exists<span class="o">(</span>C:<span class="se">\U</span>sers<span class="o">)</span> : False
</code></pre></div><h3 id="判断路径是文件还是目录">判断路径是文件还是目录</h3>
<ol>
<li><code>filesystem</code>提供了判断路径是文件还是目录的函数。（比较多）</li>
</ol>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-C++" data-lang="C++"><span class="kr">inline</span> <span class="n">_LIBCPP_INLINE_VISIBILITY</span> <span class="kt">bool</span> <span class="nf">is_block_file</span><span class="p">(</span><span class="n">file_status</span> <span class="n">__s</span><span class="p">)</span> <span class="k">noexcept</span> <span class="p">{</span>
  <span class="k">return</span> <span class="n">__s</span><span class="p">.</span><span class="n">type</span><span class="p">()</span> <span class="o">==</span> <span class="n">file_type</span><span class="o">::</span><span class="n">block</span><span class="p">;</span>
<span class="p">}</span>

<span class="kr">inline</span> <span class="n">_LIBCPP_INLINE_VISIBILITY</span> <span class="kt">bool</span> <span class="nf">is_block_file</span><span class="p">(</span><span class="k">const</span> <span class="n">path</span><span class="o">&amp;</span> <span class="n">__p</span><span class="p">)</span> <span class="p">{</span>
  <span class="k">return</span> <span class="n">is_block_file</span><span class="p">(</span><span class="n">__status</span><span class="p">(</span><span class="n">__p</span><span class="p">));</span>
<span class="p">}</span>

<span class="kr">inline</span> <span class="n">_LIBCPP_INLINE_VISIBILITY</span> <span class="kt">bool</span> <span class="nf">is_block_file</span><span class="p">(</span><span class="k">const</span> <span class="n">path</span><span class="o">&amp;</span> <span class="n">__p</span><span class="p">,</span>
                                                    <span class="n">error_code</span><span class="o">&amp;</span> <span class="n">__ec</span><span class="p">)</span> <span class="k">noexcept</span> <span class="p">{</span>
  <span class="k">return</span> <span class="n">is_block_file</span><span class="p">(</span><span class="n">__status</span><span class="p">(</span><span class="n">__p</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">__ec</span><span class="p">));</span>
<span class="p">}</span>

<span class="kr">inline</span> <span class="n">_LIBCPP_INLINE_VISIBILITY</span> <span class="kt">bool</span>
<span class="nf">is_character_file</span><span class="p">(</span><span class="n">file_status</span> <span class="n">__s</span><span class="p">)</span> <span class="k">noexcept</span> <span class="p">{</span>
  <span class="k">return</span> <span class="n">__s</span><span class="p">.</span><span class="n">type</span><span class="p">()</span> <span class="o">==</span> <span class="n">file_type</span><span class="o">::</span><span class="n">character</span><span class="p">;</span>
<span class="p">}</span>

<span class="kr">inline</span> <span class="n">_LIBCPP_INLINE_VISIBILITY</span> <span class="kt">bool</span> <span class="nf">is_character_file</span><span class="p">(</span><span class="k">const</span> <span class="n">path</span><span class="o">&amp;</span> <span class="n">__p</span><span class="p">)</span> <span class="p">{</span>
  <span class="k">return</span> <span class="n">is_character_file</span><span class="p">(</span><span class="n">__status</span><span class="p">(</span><span class="n">__p</span><span class="p">));</span>
<span class="p">}</span>

<span class="kr">inline</span> <span class="n">_LIBCPP_INLINE_VISIBILITY</span> <span class="kt">bool</span>
<span class="nf">is_character_file</span><span class="p">(</span><span class="k">const</span> <span class="n">path</span><span class="o">&amp;</span> <span class="n">__p</span><span class="p">,</span> <span class="n">error_code</span><span class="o">&amp;</span> <span class="n">__ec</span><span class="p">)</span> <span class="k">noexcept</span> <span class="p">{</span>
  <span class="k">return</span> <span class="n">is_character_file</span><span class="p">(</span><span class="n">__status</span><span class="p">(</span><span class="n">__p</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">__ec</span><span class="p">));</span>
<span class="p">}</span>

<span class="kr">inline</span> <span class="n">_LIBCPP_INLINE_VISIBILITY</span> <span class="kt">bool</span> <span class="nf">is_directory</span><span class="p">(</span><span class="n">file_status</span> <span class="n">__s</span><span class="p">)</span> <span class="k">noexcept</span> <span class="p">{</span>
  <span class="k">return</span> <span class="n">__s</span><span class="p">.</span><span class="n">type</span><span class="p">()</span> <span class="o">==</span> <span class="n">file_type</span><span class="o">::</span><span class="n">directory</span><span class="p">;</span>
<span class="p">}</span>

<span class="kr">inline</span> <span class="n">_LIBCPP_INLINE_VISIBILITY</span> <span class="kt">bool</span> <span class="nf">is_directory</span><span class="p">(</span><span class="k">const</span> <span class="n">path</span><span class="o">&amp;</span> <span class="n">__p</span><span class="p">)</span> <span class="p">{</span>
  <span class="k">return</span> <span class="n">is_directory</span><span class="p">(</span><span class="n">__status</span><span class="p">(</span><span class="n">__p</span><span class="p">));</span>
<span class="p">}</span>

<span class="kr">inline</span> <span class="n">_LIBCPP_INLINE_VISIBILITY</span> <span class="kt">bool</span> <span class="nf">is_directory</span><span class="p">(</span><span class="k">const</span> <span class="n">path</span><span class="o">&amp;</span> <span class="n">__p</span><span class="p">,</span>
                                                   <span class="n">error_code</span><span class="o">&amp;</span> <span class="n">__ec</span><span class="p">)</span> <span class="k">noexcept</span> <span class="p">{</span>
  <span class="k">return</span> <span class="n">is_directory</span><span class="p">(</span><span class="n">__status</span><span class="p">(</span><span class="n">__p</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">__ec</span><span class="p">));</span>
<span class="p">}</span>

<span class="kr">inline</span> <span class="n">_LIBCPP_INLINE_VISIBILITY</span> <span class="kt">bool</span> <span class="nf">is_empty</span><span class="p">(</span><span class="k">const</span> <span class="n">path</span><span class="o">&amp;</span> <span class="n">__p</span><span class="p">)</span> <span class="p">{</span>
  <span class="k">return</span> <span class="n">__fs_is_empty</span><span class="p">(</span><span class="n">__p</span><span class="p">);</span>
<span class="p">}</span>

<span class="kr">inline</span> <span class="n">_LIBCPP_INLINE_VISIBILITY</span> <span class="kt">bool</span> <span class="nf">is_empty</span><span class="p">(</span><span class="k">const</span> <span class="n">path</span><span class="o">&amp;</span> <span class="n">__p</span><span class="p">,</span>
                                               <span class="n">error_code</span><span class="o">&amp;</span> <span class="n">__ec</span><span class="p">)</span> <span class="p">{</span>
  <span class="k">return</span> <span class="n">__fs_is_empty</span><span class="p">(</span><span class="n">__p</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">__ec</span><span class="p">);</span>
<span class="p">}</span>

<span class="kr">inline</span> <span class="n">_LIBCPP_INLINE_VISIBILITY</span> <span class="kt">bool</span> <span class="nf">is_fifo</span><span class="p">(</span><span class="n">file_status</span> <span class="n">__s</span><span class="p">)</span> <span class="k">noexcept</span> <span class="p">{</span>
  <span class="k">return</span> <span class="n">__s</span><span class="p">.</span><span class="n">type</span><span class="p">()</span> <span class="o">==</span> <span class="n">file_type</span><span class="o">::</span><span class="n">fifo</span><span class="p">;</span>
<span class="p">}</span>
<span class="kr">inline</span> <span class="n">_LIBCPP_INLINE_VISIBILITY</span> <span class="kt">bool</span> <span class="nf">is_fifo</span><span class="p">(</span><span class="k">const</span> <span class="n">path</span><span class="o">&amp;</span> <span class="n">__p</span><span class="p">)</span> <span class="p">{</span>
  <span class="k">return</span> <span class="n">is_fifo</span><span class="p">(</span><span class="n">__status</span><span class="p">(</span><span class="n">__p</span><span class="p">));</span>
<span class="p">}</span>

<span class="kr">inline</span> <span class="n">_LIBCPP_INLINE_VISIBILITY</span> <span class="kt">bool</span> <span class="nf">is_fifo</span><span class="p">(</span><span class="k">const</span> <span class="n">path</span><span class="o">&amp;</span> <span class="n">__p</span><span class="p">,</span>
                                              <span class="n">error_code</span><span class="o">&amp;</span> <span class="n">__ec</span><span class="p">)</span> <span class="k">noexcept</span> <span class="p">{</span>
  <span class="k">return</span> <span class="n">is_fifo</span><span class="p">(</span><span class="n">__status</span><span class="p">(</span><span class="n">__p</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">__ec</span><span class="p">));</span>
<span class="p">}</span>

<span class="kr">inline</span> <span class="n">_LIBCPP_INLINE_VISIBILITY</span> <span class="kt">bool</span>
<span class="nf">is_regular_file</span><span class="p">(</span><span class="n">file_status</span> <span class="n">__s</span><span class="p">)</span> <span class="k">noexcept</span> <span class="p">{</span>
  <span class="k">return</span> <span class="n">__s</span><span class="p">.</span><span class="n">type</span><span class="p">()</span> <span class="o">==</span> <span class="n">file_type</span><span class="o">::</span><span class="n">regular</span><span class="p">;</span>
<span class="p">}</span>

<span class="kr">inline</span> <span class="n">_LIBCPP_INLINE_VISIBILITY</span> <span class="kt">bool</span> <span class="nf">is_regular_file</span><span class="p">(</span><span class="k">const</span> <span class="n">path</span><span class="o">&amp;</span> <span class="n">__p</span><span class="p">)</span> <span class="p">{</span>
  <span class="k">return</span> <span class="n">is_regular_file</span><span class="p">(</span><span class="n">__status</span><span class="p">(</span><span class="n">__p</span><span class="p">));</span>
<span class="p">}</span>

<span class="kr">inline</span> <span class="n">_LIBCPP_INLINE_VISIBILITY</span> <span class="kt">bool</span>
<span class="nf">is_regular_file</span><span class="p">(</span><span class="k">const</span> <span class="n">path</span><span class="o">&amp;</span> <span class="n">__p</span><span class="p">,</span> <span class="n">error_code</span><span class="o">&amp;</span> <span class="n">__ec</span><span class="p">)</span> <span class="k">noexcept</span> <span class="p">{</span>
  <span class="k">return</span> <span class="n">is_regular_file</span><span class="p">(</span><span class="n">__status</span><span class="p">(</span><span class="n">__p</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">__ec</span><span class="p">));</span>
<span class="p">}</span>

<span class="kr">inline</span> <span class="n">_LIBCPP_INLINE_VISIBILITY</span> <span class="kt">bool</span> <span class="nf">is_socket</span><span class="p">(</span><span class="n">file_status</span> <span class="n">__s</span><span class="p">)</span> <span class="k">noexcept</span> <span class="p">{</span>
  <span class="k">return</span> <span class="n">__s</span><span class="p">.</span><span class="n">type</span><span class="p">()</span> <span class="o">==</span> <span class="n">file_type</span><span class="o">::</span><span class="n">socket</span><span class="p">;</span>
<span class="p">}</span>

<span class="kr">inline</span> <span class="n">_LIBCPP_INLINE_VISIBILITY</span> <span class="kt">bool</span> <span class="nf">is_socket</span><span class="p">(</span><span class="k">const</span> <span class="n">path</span><span class="o">&amp;</span> <span class="n">__p</span><span class="p">)</span> <span class="p">{</span>
  <span class="k">return</span> <span class="n">is_socket</span><span class="p">(</span><span class="n">__status</span><span class="p">(</span><span class="n">__p</span><span class="p">));</span>
<span class="p">}</span>

<span class="kr">inline</span> <span class="n">_LIBCPP_INLINE_VISIBILITY</span> <span class="kt">bool</span> <span class="nf">is_socket</span><span class="p">(</span><span class="k">const</span> <span class="n">path</span><span class="o">&amp;</span> <span class="n">__p</span><span class="p">,</span>
                                                <span class="n">error_code</span><span class="o">&amp;</span> <span class="n">__ec</span><span class="p">)</span> <span class="k">noexcept</span> <span class="p">{</span>
  <span class="k">return</span> <span class="n">is_socket</span><span class="p">(</span><span class="n">__status</span><span class="p">(</span><span class="n">__p</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">__ec</span><span class="p">));</span>
<span class="p">}</span>

<span class="kr">inline</span> <span class="n">_LIBCPP_INLINE_VISIBILITY</span> <span class="kt">bool</span> <span class="nf">is_symlink</span><span class="p">(</span><span class="n">file_status</span> <span class="n">__s</span><span class="p">)</span> <span class="k">noexcept</span> <span class="p">{</span>
  <span class="k">return</span> <span class="n">__s</span><span class="p">.</span><span class="n">type</span><span class="p">()</span> <span class="o">==</span> <span class="n">file_type</span><span class="o">::</span><span class="n">symlink</span><span class="p">;</span>
<span class="p">}</span>

<span class="kr">inline</span> <span class="n">_LIBCPP_INLINE_VISIBILITY</span> <span class="kt">bool</span> <span class="nf">is_symlink</span><span class="p">(</span><span class="k">const</span> <span class="n">path</span><span class="o">&amp;</span> <span class="n">__p</span><span class="p">)</span> <span class="p">{</span>
  <span class="k">return</span> <span class="n">is_symlink</span><span class="p">(</span><span class="n">__symlink_status</span><span class="p">(</span><span class="n">__p</span><span class="p">));</span>
<span class="p">}</span>

<span class="kr">inline</span> <span class="n">_LIBCPP_INLINE_VISIBILITY</span> <span class="kt">bool</span> <span class="nf">is_symlink</span><span class="p">(</span><span class="k">const</span> <span class="n">path</span><span class="o">&amp;</span> <span class="n">__p</span><span class="p">,</span>
                                                 <span class="n">error_code</span><span class="o">&amp;</span> <span class="n">__ec</span><span class="p">)</span> <span class="k">noexcept</span> <span class="p">{</span>
  <span class="k">return</span> <span class="n">is_symlink</span><span class="p">(</span><span class="n">__symlink_status</span><span class="p">(</span><span class="n">__p</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">__ec</span><span class="p">));</span>
<span class="p">}</span>

<span class="kr">inline</span> <span class="n">_LIBCPP_INLINE_VISIBILITY</span> <span class="kt">bool</span> <span class="nf">is_other</span><span class="p">(</span><span class="n">file_status</span> <span class="n">__s</span><span class="p">)</span> <span class="k">noexcept</span> <span class="p">{</span>
  <span class="k">return</span> <span class="n">exists</span><span class="p">(</span><span class="n">__s</span><span class="p">)</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="n">is_regular_file</span><span class="p">(</span><span class="n">__s</span><span class="p">)</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="n">is_directory</span><span class="p">(</span><span class="n">__s</span><span class="p">)</span> <span class="o">&amp;&amp;</span>
         <span class="o">!</span><span class="n">is_symlink</span><span class="p">(</span><span class="n">__s</span><span class="p">);</span>
<span class="p">}</span>

<span class="kr">inline</span> <span class="n">_LIBCPP_INLINE_VISIBILITY</span> <span class="kt">bool</span> <span class="nf">is_other</span><span class="p">(</span><span class="k">const</span> <span class="n">path</span><span class="o">&amp;</span> <span class="n">__p</span><span class="p">)</span> <span class="p">{</span>
  <span class="k">return</span> <span class="n">is_other</span><span class="p">(</span><span class="n">__status</span><span class="p">(</span><span class="n">__p</span><span class="p">));</span>
<span class="p">}</span>

<span class="kr">inline</span> <span class="n">_LIBCPP_INLINE_VISIBILITY</span> <span class="kt">bool</span> <span class="nf">is_other</span><span class="p">(</span><span class="k">const</span> <span class="n">path</span><span class="o">&amp;</span> <span class="n">__p</span><span class="p">,</span>
                                               <span class="n">error_code</span><span class="o">&amp;</span> <span class="n">__ec</span><span class="p">)</span> <span class="k">noexcept</span> <span class="p">{</span>
  <span class="k">return</span> <span class="n">is_other</span><span class="p">(</span><span class="n">__status</span><span class="p">(</span><span class="n">__p</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">__ec</span><span class="p">));</span>
<span class="p">}</span>
</code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-C++" data-lang="C++"><span class="kt">void</span> <span class="nf">eg2_5</span><span class="p">()</span> <span class="p">{</span>
    <span class="cm">/*判断路径是文件还是目录*/</span>
    <span class="n">std</span><span class="o">::</span><span class="n">filesystem</span><span class="o">::</span><span class="n">path</span> <span class="n">file_path</span> <span class="o">=</span> <span class="s">&#34;../test_dir/1.txt&#34;</span><span class="p">;</span>
    <span class="n">std</span><span class="o">::</span><span class="n">filesystem</span><span class="o">::</span><span class="n">path</span> <span class="n">dir_path</span> <span class="o">=</span> <span class="s">&#34;../test_dir/1_dir&#34;</span><span class="p">;</span>
    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;is_regular_file(file_path) : &#34;</span>
              <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">filesystem</span><span class="o">::</span><span class="n">is_regular_file</span><span class="p">(</span><span class="n">file_path</span><span class="p">)</span>
              <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;is_directory(dir_path) : &#34;</span>
              <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">filesystem</span><span class="o">::</span><span class="n">is_directory</span><span class="p">(</span><span class="n">dir_path</span><span class="p">)</span>
              <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell">is_regular_file<span class="o">(</span>file_path<span class="o">)</span> : <span class="m">1</span>
is_directory<span class="o">(</span>dir_path<span class="o">)</span> : <span class="m">1</span>
</code></pre></div><ol start="2">
<li><code>os.path</code>中提供了函数<code>isfile()</code>和<code>isdir()</code>。</li>
</ol>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="k">def</span> <span class="nf">eg2_5</span><span class="p">():</span>
    <span class="s2">&#34;&#34;&#34;判断路径是文件还是目录&#34;&#34;&#34;</span>
    <span class="n">file_path</span> <span class="o">=</span> <span class="s2">&#34;./test_dir/1.txt&#34;</span>
    <span class="n">dir_path</span> <span class="o">=</span> <span class="s2">&#34;./test_dir/1_dir&#34;</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&#34;os.path.isfile(</span><span class="si">{}</span><span class="s2">) : </span><span class="si">{}</span><span class="s2">&#34;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">file_path</span><span class="p">,</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="n">file_path</span><span class="p">)))</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&#34;os.path.isdir(</span><span class="si">{}</span><span class="s2">) : </span><span class="si">{}</span><span class="s2">&#34;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">dir_path</span><span class="p">,</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isdir</span><span class="p">(</span><span class="n">dir_path</span><span class="p">)))</span>
</code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell">os.path.isfile<span class="o">(</span>./test_dir/1.txt<span class="o">)</span> : True
os.path.isdir<span class="o">(</span>./test_dir/1_dir<span class="o">)</span> : True
</code></pre></div><h3 id="返回绝对路径神奇">返回绝对路径【神奇】</h3>
<ol>
<li><code>filesystem</code>中提供了函数<code>absolute()</code>。</li>
</ol>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-C++" data-lang="C++"><span class="kt">void</span> <span class="nf">eg2_6</span><span class="p">()</span> <span class="p">{</span>
    <span class="cm">/*返回绝对路径*/</span>
    <span class="n">std</span><span class="o">::</span><span class="n">filesystem</span><span class="o">::</span><span class="n">path</span> <span class="n">path</span> <span class="o">=</span> <span class="s">&#34;../test_dir&#34;</span><span class="p">;</span>
    <span class="n">std</span><span class="o">::</span><span class="n">filesystem</span><span class="o">::</span><span class="n">path</span> <span class="n">abs_path</span> <span class="o">=</span> <span class="n">std</span><span class="o">::</span><span class="n">filesystem</span><span class="o">::</span><span class="n">absolute</span><span class="p">(</span><span class="n">path</span><span class="p">);</span>
    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;abs_path : &#34;</span> <span class="o">&lt;&lt;</span> <span class="n">abs_path</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;exists(abs_path) : &#34;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">filesystem</span><span class="o">::</span><span class="n">exists</span><span class="p">(</span><span class="n">abs_path</span><span class="p">)</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell">abs_path : <span class="s2">&#34;/Users/xxx/Github/intro_to_C-python/xxx/cmake-build-debug/../test_dir&#34;</span>
exists<span class="o">(</span>abs_path<span class="o">)</span> : <span class="m">1</span>
</code></pre></div><ol start="2">
<li>在Windows系统中结果如下。</li>
</ol>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell">abs_path : <span class="s2">&#34;D:\\GitHub\\intro_to_C-python\\xxx\\test_dir&#34;</span>
exists<span class="o">(</span>abs_path<span class="o">)</span> : <span class="m">1</span>
</code></pre></div><ol start="3">
<li><code>os.path</code>中提供了函数<code>abspath()</code>。</li>
</ol>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="k">def</span> <span class="nf">eg2_6</span><span class="p">():</span>
    <span class="s2">&#34;&#34;&#34;返回绝对路径&#34;&#34;&#34;</span>
    <span class="n">path</span> <span class="o">=</span> <span class="s2">&#34;../../-PyTorch-&#34;</span>
    <span class="n">abs_path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">abspath</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&#34;abs_path : </span><span class="si">{}</span><span class="s2">&#34;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">abs_path</span><span class="p">))</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&#34;os.path.exists(</span><span class="si">{}</span><span class="s2">) : </span><span class="si">{}</span><span class="s2">&#34;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">abs_path</span><span class="p">,</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">abs_path</span><span class="p">)))</span>
</code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell">abs_path : /Users/xxx/Github/-PyTorch-
os.path.exists<span class="o">(</span>/Users/xxx/Github/-PyTorch-<span class="o">)</span> : True
</code></pre></div><h2 id="filesystem-vs-shutil"><code>filesystem</code> vs. <code>shutil</code></h2>
<p><code>shutil = shell + util</code>，对os进行一些补充。</p>
<h3 id="文件拷贝">文件拷贝</h3>
<ol>
<li><code>filesystem</code>中提供了<code>copy_file()</code>函数，可以拷贝文件或空文件夹。</li>
</ol>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-C++" data-lang="C++"><span class="kt">void</span> <span class="nf">eg3_1</span><span class="p">()</span> <span class="p">{</span>
    <span class="cm">/*文件拷贝*/</span>
    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;~~~~~~before copy_file~~~~~~&#34;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
    <span class="k">for</span> <span class="p">(</span><span class="k">const</span> <span class="k">auto</span> <span class="o">&amp;</span><span class="nl">i</span> <span class="p">:</span><span class="n">std</span><span class="o">::</span><span class="n">filesystem</span><span class="o">::</span><span class="n">directory_iterator</span><span class="p">(</span><span class="s">&#34;../test_dir&#34;</span><span class="p">))</span> <span class="p">{</span>
        <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">i</span><span class="p">.</span><span class="n">path</span><span class="p">()</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="n">std</span><span class="o">::</span><span class="n">filesystem</span><span class="o">::</span><span class="n">copy_file</span><span class="p">(</span><span class="s">&#34;../test_dir/1.txt&#34;</span><span class="p">,</span> <span class="s">&#34;../test_dir/eg3_1.txt&#34;</span><span class="p">);</span>
    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;~~~~~~after copy_file~~~~~~&#34;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
    <span class="k">for</span> <span class="p">(</span><span class="k">const</span> <span class="k">auto</span> <span class="o">&amp;</span><span class="nl">i</span> <span class="p">:</span><span class="n">std</span><span class="o">::</span><span class="n">filesystem</span><span class="o">::</span><span class="n">directory_iterator</span><span class="p">(</span><span class="s">&#34;../test_dir&#34;</span><span class="p">))</span> <span class="p">{</span>
        <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">i</span><span class="p">.</span><span class="n">path</span><span class="p">()</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell">~~~~~~before copy_file~~~~~~
<span class="s2">&#34;../test_dir/1.txt&#34;</span>
<span class="s2">&#34;../test_dir/1_dir&#34;</span>
~~~~~~after copy_file~~~~~~
<span class="s2">&#34;../test_dir/eg3_1.txt&#34;</span>
<span class="s2">&#34;../test_dir/1.txt&#34;</span>
<span class="s2">&#34;../test_dir/1_dir&#34;</span>
</code></pre></div><ol start="2">
<li><code>shutil</code>中提供函数<code>copyfile()</code>。</li>
</ol>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="k">def</span> <span class="nf">eg3_1</span><span class="p">():</span>
    <span class="s2">&#34;&#34;&#34;文件拷贝&#34;&#34;&#34;</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&#34;~~~~~~before copy_file~~~~~~&#34;</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="s2">&#34;./test_dir&#34;</span><span class="p">):</span>
        <span class="nb">print</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
    <span class="n">src_path</span> <span class="o">=</span> <span class="s2">&#34;./test_dir/1.txt&#34;</span>
    <span class="n">dst_path</span> <span class="o">=</span> <span class="s2">&#34;./test_dir/eg3_1.txt&#34;</span>
    <span class="n">shutil</span><span class="o">.</span><span class="n">copyfile</span><span class="p">(</span><span class="n">src_path</span><span class="p">,</span> <span class="n">dst_path</span><span class="p">)</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&#34;~~~~~~after copy_file~~~~~~&#34;</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="s2">&#34;./test_dir&#34;</span><span class="p">):</span>
        <span class="nb">print</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
</code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell">~~~~~~before copy_file~~~~~~
1.txt
1_dir
~~~~~~after copy_file~~~~~~
eg3_1.txt
1.txt
1_dir
</code></pre></div><h3 id="文件删除">文件删除</h3>
<ol>
<li><code>filesystem</code>中提供了<code>remove()</code>函数，可以删除文件或空文件夹。</li>
</ol>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-C++" data-lang="C++"><span class="kt">void</span> <span class="nf">eg3_2</span><span class="p">()</span> <span class="p">{</span>
    <span class="cm">/*文件删除*/</span>
    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;~~~~~~before remove~~~~~~&#34;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
    <span class="k">for</span> <span class="p">(</span><span class="k">const</span> <span class="k">auto</span> <span class="o">&amp;</span><span class="nl">i</span> <span class="p">:</span><span class="n">std</span><span class="o">::</span><span class="n">filesystem</span><span class="o">::</span><span class="n">directory_iterator</span><span class="p">(</span><span class="s">&#34;../test_dir&#34;</span><span class="p">))</span> <span class="p">{</span>
        <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">i</span><span class="p">.</span><span class="n">path</span><span class="p">()</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="n">std</span><span class="o">::</span><span class="n">filesystem</span><span class="o">::</span><span class="n">remove</span><span class="p">(</span><span class="s">&#34;../test_dir/eg3_1.txt&#34;</span><span class="p">);</span>
    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;~~~~~~after remove~~~~~~&#34;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
    <span class="k">for</span> <span class="p">(</span><span class="k">const</span> <span class="k">auto</span> <span class="o">&amp;</span><span class="nl">i</span> <span class="p">:</span><span class="n">std</span><span class="o">::</span><span class="n">filesystem</span><span class="o">::</span><span class="n">directory_iterator</span><span class="p">(</span><span class="s">&#34;../test_dir&#34;</span><span class="p">))</span> <span class="p">{</span>
        <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">i</span><span class="p">.</span><span class="n">path</span><span class="p">()</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell">~~~~~~before remove~~~~~~
<span class="s2">&#34;../test_dir/eg3_1.txt&#34;</span>
<span class="s2">&#34;../test_dir/1.txt&#34;</span>
<span class="s2">&#34;../test_dir/1_dir&#34;</span>
~~~~~~after remove~~~~~~
<span class="s2">&#34;../test_dir/1.txt&#34;</span>
<span class="s2">&#34;../test_dir/1_dir&#34;</span>
</code></pre></div><ol start="2">
<li><code>os</code>中提供了函数<code>remove()</code>。</li>
</ol>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="k">def</span> <span class="nf">eg3_2</span><span class="p">():</span>
    <span class="s2">&#34;&#34;&#34;文件删除&#34;&#34;&#34;</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&#34;~~~~~~before remove~~~~~~&#34;</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="s2">&#34;./test_dir&#34;</span><span class="p">):</span>
        <span class="nb">print</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
    <span class="n">rm_path</span> <span class="o">=</span> <span class="s2">&#34;./test_dir/eg3_1.txt&#34;</span>
    <span class="n">os</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">rm_path</span><span class="p">)</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&#34;~~~~~~after remove~~~~~~&#34;</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="s2">&#34;./test_dir&#34;</span><span class="p">):</span>
        <span class="nb">print</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
</code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell">~~~~~~before remove~~~~~~
eg3_1.txt
1.txt
1_dir
~~~~~~after remove~~~~~~
1.txt
1_dir
</code></pre></div><h3 id="路径拷贝">路径拷贝</h3>
<ol>
<li><code>filesystem</code>中提供了<code>copy()</code>函数，可以按照选项（是否递归）拷贝文件或文件夹。</li>
</ol>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-C++" data-lang="C++"><span class="kt">void</span> <span class="nf">eg3_3</span><span class="p">()</span> <span class="p">{</span>
    <span class="cm">/*路径拷贝*/</span>
    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;~~~~~~before copy[1_dir]~~~~~~&#34;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
    <span class="k">for</span> <span class="p">(</span><span class="k">const</span> <span class="k">auto</span> <span class="o">&amp;</span><span class="nl">i</span> <span class="p">:</span><span class="n">std</span><span class="o">::</span><span class="n">filesystem</span><span class="o">::</span><span class="n">directory_iterator</span><span class="p">(</span><span class="s">&#34;../test_dir/1_dir&#34;</span><span class="p">))</span> <span class="p">{</span>
        <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">i</span><span class="p">.</span><span class="n">path</span><span class="p">()</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="n">std</span><span class="o">::</span><span class="n">filesystem</span><span class="o">::</span><span class="n">copy</span><span class="p">(</span><span class="s">&#34;../test_dir/1_dir&#34;</span><span class="p">,</span>
                          <span class="s">&#34;../test_dir/eg3_3_dir&#34;</span><span class="p">,</span>
                          <span class="n">std</span><span class="o">::</span><span class="n">filesystem</span><span class="o">::</span><span class="n">copy_options</span><span class="o">::</span><span class="n">recursive</span><span class="p">);</span>
    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;~~~~~~after copy[eg3_3_dir]~~~~~~&#34;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
    <span class="k">for</span> <span class="p">(</span><span class="k">const</span> <span class="k">auto</span> <span class="o">&amp;</span><span class="nl">i</span> <span class="p">:</span><span class="n">std</span><span class="o">::</span><span class="n">filesystem</span><span class="o">::</span><span class="n">directory_iterator</span><span class="p">(</span><span class="s">&#34;../test_dir/eg3_3_dir&#34;</span><span class="p">))</span> <span class="p">{</span>
        <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">i</span><span class="p">.</span><span class="n">path</span><span class="p">()</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell">~~~~~~before copy<span class="o">[</span>1_dir<span class="o">]</span>~~~~~~
<span class="s2">&#34;../test_dir/1_dir/empty_dir&#34;</span>
<span class="s2">&#34;../test_dir/1_dir/2.txt&#34;</span>
~~~~~~after copy<span class="o">[</span>eg3_3_dir<span class="o">]</span>~~~~~~
<span class="s2">&#34;../test_dir/eg3_3_dir/empty_dir&#34;</span>
<span class="s2">&#34;../test_dir/eg3_3_dir/2.txt&#34;</span>
</code></pre></div><ol start="2">
<li><code>shutil</code>提供了函数<code>copytree()</code>。</li>
</ol>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="k">def</span> <span class="nf">eg3_3</span><span class="p">():</span>
    <span class="s2">&#34;&#34;&#34;路径拷贝&#34;&#34;&#34;</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&#34;~~~~~~before copy[1_dir]~~~~~~&#34;</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="s2">&#34;./test_dir/1_dir&#34;</span><span class="p">):</span>
        <span class="nb">print</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
    <span class="n">src_path</span> <span class="o">=</span> <span class="s2">&#34;./test_dir/1_dir&#34;</span>
    <span class="n">dst_path</span> <span class="o">=</span> <span class="s2">&#34;./test_dir/eg3_3_dir&#34;</span>
    <span class="n">shutil</span><span class="o">.</span><span class="n">copytree</span><span class="p">(</span><span class="n">src_path</span><span class="p">,</span> <span class="n">dst_path</span><span class="p">)</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&#34;~~~~~~after copy[eg3_3_dir]~~~~~~&#34;</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="s2">&#34;./test_dir/eg3_3_dir&#34;</span><span class="p">):</span>
        <span class="nb">print</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
</code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell">~~~~~~before copy<span class="o">[</span>1_dir<span class="o">]</span>~~~~~~
empty_dir
2.txt
~~~~~~after copy<span class="o">[</span>eg3_3_dir<span class="o">]</span>~~~~~~
empty_dir
2.txt
</code></pre></div><h3 id="递归删除">递归删除</h3>
<ol>
<li><code>filesystem</code>中提供了<code>remove_all()</code>函数，可以递归删除文件或文件夹。</li>
</ol>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-C++" data-lang="C++"><span class="kt">void</span> <span class="nf">eg3_4</span><span class="p">()</span> <span class="p">{</span>
    <span class="cm">/*递归删除*/</span>
    <span class="n">std</span><span class="o">::</span><span class="n">filesystem</span><span class="o">::</span><span class="n">path</span> <span class="n">dir_path</span> <span class="o">=</span> <span class="s">&#34;../test_dir/eg3_3_dir&#34;</span><span class="p">;</span>
    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;exists(dir_path) : &#34;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">filesystem</span><span class="o">::</span><span class="n">exists</span><span class="p">(</span><span class="n">dir_path</span><span class="p">)</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
    <span class="n">std</span><span class="o">::</span><span class="n">filesystem</span><span class="o">::</span><span class="n">remove_all</span><span class="p">(</span><span class="n">dir_path</span><span class="p">);</span>
    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;exists(dir_path) : &#34;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">filesystem</span><span class="o">::</span><span class="n">exists</span><span class="p">(</span><span class="n">dir_path</span><span class="p">)</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell">exists<span class="o">(</span>dir_path<span class="o">)</span> : <span class="m">1</span>
exists<span class="o">(</span>dir_path<span class="o">)</span> : <span class="m">0</span>
</code></pre></div><ol start="2">
<li><code>shutil</code>提供了函数<code>rmtree()</code>。</li>
</ol>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="k">def</span> <span class="nf">eg3_4</span><span class="p">():</span>
    <span class="s2">&#34;&#34;&#34;递归删除&#34;&#34;&#34;</span>
    <span class="n">dir_path</span> <span class="o">=</span> <span class="s2">&#34;./test_dir/eg3_3_dir&#34;</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&#34;os.path.exists(</span><span class="si">{}</span><span class="s2">) : </span><span class="si">{}</span><span class="s2">&#34;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">dir_path</span><span class="p">,</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">dir_path</span><span class="p">)))</span>
    <span class="n">shutil</span><span class="o">.</span><span class="n">rmtree</span><span class="p">(</span><span class="n">dir_path</span><span class="p">)</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&#34;os.path.exists(</span><span class="si">{}</span><span class="s2">) : </span><span class="si">{}</span><span class="s2">&#34;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">dir_path</span><span class="p">,</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">dir_path</span><span class="p">)))</span>
</code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell">os.path.exists<span class="o">(</span>./test_dir/eg3_3_dir<span class="o">)</span> : True
os.path.exists<span class="o">(</span>./test_dir/eg3_3_dir<span class="o">)</span> : False
</code></pre></div></div><div class="post-footer" id="post-footer">
    <div class="post-info"><div class="post-info-tag"><span><a href="/tags/c&#43;&#43;%E4%B8%8Epython%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F%E5%AF%B9%E6%AF%94/">C&#43;&#43;与python文件系统对比</a>
                </span></div><div class="post-info-line"><div class="post-info-mod">
                <span>更新于 2022-01-25</span>
            </div><div class="post-info-mod"></div>
        </div><div class="post-info-share">
            <span><a href="javascript:void(0);" title="分享到 Twitter" data-sharer="twitter" data-url="https://acking-you.github.io/posts/c&#43;&#43;%E4%B8%8Epython%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F%E5%AF%B9%E6%AF%94/" data-title="C&#43;&#43;与python文件系统对比" data-hashtags="C&#43;&#43;与python文件系统对比"><i class="fab fa-twitter fa-fw"></i></a><a href="javascript:void(0);" title="分享到 Facebook" data-sharer="facebook" data-url="https://acking-you.github.io/posts/c&#43;&#43;%E4%B8%8Epython%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F%E5%AF%B9%E6%AF%94/" data-hashtag="C&#43;&#43;与python文件系统对比"><i class="fab fa-facebook-square fa-fw"></i></a><a href="javascript:void(0);" title="分享到 WhatsApp" data-sharer="whatsapp" data-url="https://acking-you.github.io/posts/c&#43;&#43;%E4%B8%8Epython%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F%E5%AF%B9%E6%AF%94/" data-title="C&#43;&#43;与python文件系统对比" data-web><i class="fab fa-whatsapp fa-fw"></i></a><a href="javascript:void(0);" title="分享到 Line" data-sharer="line" data-url="https://acking-you.github.io/posts/c&#43;&#43;%E4%B8%8Epython%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F%E5%AF%B9%E6%AF%94/" data-title="C&#43;&#43;与python文件系统对比"><i class="fab fa-line fa-fw"></i></a><a href="javascript:void(0);" title="分享到 微博" data-sharer="weibo" data-url="https://acking-you.github.io/posts/c&#43;&#43;%E4%B8%8Epython%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F%E5%AF%B9%E6%AF%94/" data-title="C&#43;&#43;与python文件系统对比" data-image="https://img-blog.csdnimg.cn/img_convert/2319540b52ba53f88ff0afbf5cabb92c.png#pic_center"><i class="fab fa-weibo fa-fw"></i></a><a href="javascript:void(0);" title="分享到 Myspace" data-sharer="myspace" data-url="https://acking-you.github.io/posts/c&#43;&#43;%E4%B8%8Epython%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F%E5%AF%B9%E6%AF%94/" data-title="C&#43;&#43;与python文件系统对比" data-description="C&#43;&#43;与python文件系统对比"><i data-svg-src="/lib/simple-icons/icons/myspace.min.svg"></i></a><a href="javascript:void(0);" title="分享到 Blogger" data-sharer="blogger" data-url="https://acking-you.github.io/posts/c&#43;&#43;%E4%B8%8Epython%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F%E5%AF%B9%E6%AF%94/" data-title="C&#43;&#43;与python文件系统对比" data-description="C&#43;&#43;与python文件系统对比"><i class="fab fa-blogger fa-fw"></i></a><a href="javascript:void(0);" title="分享到 Evernote" data-sharer="evernote" data-url="https://acking-you.github.io/posts/c&#43;&#43;%E4%B8%8Epython%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F%E5%AF%B9%E6%AF%94/" data-title="C&#43;&#43;与python文件系统对比"><i class="fab fa-evernote fa-fw"></i></a></span>
        </div></div><div class="post-nav"><a href="/posts/java-io/" class="prev" rel="prev" title="Java I/O"><i class="fas fa-angle-left fa-fw"></i>Previous Post</a>
            <a href="/posts/tcp%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B%E4%B8%ADconnectlistenaccept%E4%B8%89%E8%80%85%E4%B9%8B%E9%97%B4%E7%9A%84%E5%85%B3%E7%B3%BB/" class="next" rel="next" title="TCP网络编程中connect、listen、accept三者之间的关系">Next Post<i class="fas fa-angle-right fa-fw"></i></a></div></div>
</div></article></div>
            </main>
            <footer class="footer"><div class="footer-container"><div class="footer-line">由 <a href="https://gohugo.io/" target="_blank" rel="noopener noreffer" title="Hugo 0.86.0">Hugo</a> 强力驱动 | 主题 - <a href="https://github.com/khusika/FeelIt" target="_blank" rel="noopener noreffer" title="FeelIt 1.0.1"><i class="fas fa-hand-holding-heart fa-fw"></i> FeelIt</a>
        </div><div class="footer-line" itemscope itemtype="http://schema.org/CreativeWork"><i class="far fa-copyright fa-fw"></i><span itemprop="copyrightYear">2023</span><span class="author" itemprop="copyrightHolder">&nbsp;<a href="/"></a></span></div>
</div>
</footer>
        </div>

        <div id="fixed-buttons"><a href="#" id="back-to-top" class="fixed-button" title="回到顶部">
                <i class="fas fa-chevron-up fa-fw"></i>
            </a></div><link rel="stylesheet" href="/lib/fontawesome-free/all.min.css"><link rel="stylesheet" href="/lib/animate/animate.min.css"><link rel="stylesheet" href="/lib/katex/katex.min.css"><link rel="stylesheet" href="/lib/katex/copy-tex.min.css"><script src="/lib/autocomplete/autocomplete.min.js"></script><script src="/lib/lunr/lunr.min.js"></script><script src="/lib/lunr/lunr.stemmer.support.min.js"></script><script src="/lib/lunr/lunr.zh.min.js"></script><script src="/lib/lazysizes/lazysizes.min.js"></script><script src="/lib/clipboard/clipboard.min.js"></script><script src="/lib/sharer/sharer.min.js"></script><script src="/lib/katex/katex.min.js"></script><script src="/lib/katex/auto-render.min.js"></script><script src="/lib/katex/copy-tex.min.js"></script><script src="/lib/katex/mhchem.min.js"></script><script>window.config={"code":{"copyTitle":"复制到剪贴板","maxShownLines":200},"comment":{},"math":{"delimiters":[{"display":true,"left":"$$","right":"$$"},{"display":true,"left":"\\[","right":"\\]"},{"display":false,"left":"$","right":"$"},{"display":false,"left":"\\(","right":"\\)"}],"strict":false},"search":{"highlightTag":"em","lunrIndexURL":"/index.json","lunrLanguageCode":"zh","lunrSegmentitURL":"/lib/lunr/lunr.segmentit.js","maxResultLength":100,"noResultsFound":"没有找到结果","snippetLength":50,"type":"lunr"}};</script><script src="/js/theme.min.js"></script></body></html>
